package com.example.mybank.springSecurity;

import com.example.mybank.tool.JsonResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.example.mybank.tool.ResultCode;

@Slf4j
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Autowired
    ObjectMapper objectMapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {

        //返回json数据
        JsonResult jsonResult=null;
        if (e instanceof UsernameNotFoundException) {
            //账号过期
            jsonResult=JsonResult.failWithCode(ResultCode.USER_NOT_EXIST);
        } else {
            jsonResult=JsonResult.fail(e);
        }
        //处理编码方式，防止中文乱码的情况
        httpServletResponse.setContentType("text/json;charset=utf-8");
        //塞到HttpServletResponse中返回给前台
        HttpServletResponse response=httpServletResponse;
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(jsonResult));

//        JsonResult result = null;
//        if (e instanceof AccountExpiredException) {
//            //账号过期
//            result = ResultTool.fail(ResultCode.USER_ACCOUNT_EXPIRED);
//        } else if (e instanceof BadCredentialsException) {
//            //密码错误
//            result = ResultTool.fail(ResultCode.USER_CREDENTIALS_ERROR);
//        } else if (e instanceof CredentialsExpiredException) {
//            //密码过期
//            result = ResultTool.fail(ResultCode.USER_CREDENTIALS_EXPIRED);
//        } else if (e instanceof DisabledException) {
//            //账号不可用
//            result = ResultTool.fail(ResultCode.USER_ACCOUNT_DISABLE);
//        } else if (e instanceof LockedException) {
//            //账号锁定
//            result = ResultTool.fail(ResultCode.USER_ACCOUNT_LOCKED);
//        } else if (e instanceof InternalAuthenticationServiceException) {
//            //用户不存在
//            result = ResultTool.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
//        }else{
//            //其他错误
//            result = ResultTool.fail(ResultCode.COMMON_FAIL);
//        }
    }
}